package com.foreveross.ehcache;

import java.net.URL;
import java.util.Date;
import java.util.Iterator;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;

/**
 * @author luofangyi
 * 缓存操作
 */
public class CacheUtil {

	private static final String DEFAULT_CLASSPATH_CONFIGURATION_FILE = "/ehcache.xml";
	
	private static CacheUtil cacheUtil = null;
	
	public static CacheUtil getInstance(){
		if(cacheUtil == null){
			return new CacheUtil();
		}
		return cacheUtil;
	}
	/**
	 * @author luofangyi
	 * 缓存名称
	 */
	public enum CacheName{
		/**
		 * 任务缓存
		 */
		TASK(0, "TASK");
		private int type;
		private String value;
		private CacheName(int type, String value){
			this.type = type;
			this.value = value;
		}
		public int getType() {
			return type;
		}
		public void setType(int type) {
			this.type = type;
		}
		public String getValue() {
			return value;
		}
		public void setValue(String value) {
			this.value = value;
		}
	}

	/**
	 * 查找目标缓存, 如果不存在则返回空值
	 * @param key
	 * @return
	 */
	public static Object getStringValueInCache(String key, CacheName cacheName) {
		Cache cache = getCache(cacheName);
		Element element = cache.get(key);
		return element != null ? element.getObjectValue() : null;
	}
	
	public static Object getStringValueInCache(String key) {
		Cache cache = getCache(CacheName.TASK);
		Element element = cache.get(key);
		return element != null ? element.getObjectValue() : null;
	}
	
	/**
	 * 查找指定时间内被访问过的目标缓存，如果不存在或指定时间内没有被访问，返回null
	 * @param key
	 * @param cacheName
	 * @param activeTime
	 * @return
	 */
	public static String getActiveStringValueInCache(String key, CacheName cacheName, long activeTime) {
		Cache cache = getCache(cacheName);
		Element element = cache.get(key);
		String StringValue = "";
		if (element != null) {
			long lastAccessTime = element.getNextToLastAccessTime();//上次访问时间点
			long activeTimelimit = lastAccessTime + activeTime;//有效时间点
			long currentTime = new Date().getTime();//当前时间点
			if (currentTime < activeTimelimit || lastAccessTime == 0) {
				StringValue = (String)element.getValue();
			} else {
				//失效后清除该缓存
				cache.remove(key);
			}
		}
		return StringValue;
	}
	
	public static String getActiveStringValueInCache(String key, long activeTime) {
		Cache cache = getCache(CacheName.TASK);
		Element element = cache.get(key);
		String StringValue = "";
		if (element != null) {
			long lastAccessTime = element.getNextToLastAccessTime();//上次访问时间点
			long activeTimelimit = lastAccessTime + activeTime;//有效时间点
			long currentTime = new Date().getTime();//当前时间点
			if (currentTime < activeTimelimit || lastAccessTime == 0) {
				StringValue = (String)element.getValue();
			} else {
				//失效后清除该缓存
				cache.remove(key);
			}
		}
		return StringValue;
	}
	
	/**
	 * 保存缓存
	 * @param key
	 * @return
	 */
	public static void putStringValueToCache(String key, Object value, CacheName cacheName) {
		Cache cache = getCache(cacheName);
		cache.put(new Element(key, value));
	}
	
	public static void putStringValueToCache(String key, Object value) {
		Cache cache = getCache(CacheName.TASK);
		cache.put(new Element(key, value));
	}
	
	/**
	 * 返回指定缓存，如果为空，则创建一个。
	 * @param key
	 * @return
	 */
	private static Cache getCache(CacheName cacheName) {
		URL url = CacheUtil.class.getResource("/ehcache-crawl.xml");
		CacheManager cacheManager = CacheManager.create(url);   
		Cache cache = cacheManager.getCache(cacheName.getValue());
		CacheConfiguration config = new CacheConfiguration();
		
	    if (cache == null)
	    {
	    	cache = new Cache(cacheName.getValue(), 100000, true, true, 0, 0);  
	        cacheManager.addCache(cache);
	    }
	    return cache;
	}
	
	private Ehcache getCache(){
//		URL url = CacheUtil.class.getResource("/ehcache-crawl.xml");
//		URL url = CacheUtil.class.getResource("/ehcache.xml");
		CacheManager cacheManager = CacheManager.getInstance();//.create(url);
		Ehcache cache = cacheManager.getEhcache("taskExceptions");
		
		return cache;
	}
	
	public void putObjectToCache(String key, Object value) {
		Element elem = new Element(key, value);
		getCache().put(elem);
	}
	
	public Object getObjectFromCache(String key){
		Ehcache ehcache = getCache();
		Element element = ehcache.get(key);
		return element != null ? element.getObjectValue() : null;
	}
	
	public void removeAllCache(){
		getCache().removeAll();
	}
}
